package com.buba.service.impl;

import com.buba.dao.EmployeeMapper;
import com.buba.pojo.Employee;
import com.buba.service.EmployeeService;
import com.buba.utils.PasswordUtil;
import com.buba.utils.SystemConstant;
import com.buba.utils.UUIDUtils;
import com.buba.vo.EmployeeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service()
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public Employee getEmpByloginnameAndloginpwd(Employee employee) {
        Employee emp=employeeMapper.getEmpByName(employee.getLoginname());
        String salt=emp.getSalt();
        String pwd=PasswordUtil.md5(employee.getLoginpwd(),salt,5);
        employee.setLoginpwd(pwd);
        return employeeMapper.getEmpByloginnameAndloginpwd(employee);
    }

    /**
     * todo 查询员工数量
     * @param deptId
     * @return
     */
    public int getEmployeeCountByDeptId(Integer deptId) {
        return employeeMapper.getEmployeeCountByDeptId(deptId);
    }

    /**
     * todo 查询角色数量
     * @param roleId
     * @return
     */
    public int getEmployeeCountByRoleId(Integer roleId) {
        return employeeMapper.getEmployeeCountByRoleId(roleId);
    }
    /**
     * todo 查询员工列表
     * @param employeeVO
     * @return
     */
    public List<Employee> findEmployeeList(EmployeeVO employeeVO) {
        return employeeMapper.findEmployeeList(employeeVO);
    }

    /**
     * todo 添加员工实现方法
     *
     * @param employee
     * @return
     */
    public int addEmployee(Employee employee) {
        {
            employee.setSalt(UUIDUtils.randomUUID());//加密盐值
            employee.setCreateDate(new Date());//创建时间
            employee.setLoginpwd(PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD, employee.getSalt(), SystemConstant.PASSWORD_COUNT));//密码
            return employeeMapper.addEmployee(employee);
        }
    }

    /**
     * todo 修改员工
     * @param employee
     * @return
     */
    public int updateEmployee(Employee employee) {

        employee.setModifyDate(new Date());
        return employeeMapper.updateEmployee(employee);
    }
    /**
     * todo 删除员工
     * @param id
     * @return
     */
    public int deleteById(Integer id) {

        //删除员工角色关系表的数据
        employeeMapper.deleteEmployeeAndRole(id);
        //调用删除员工的方法
        return employeeMapper.deleteById(id);
    }



    /**
     * todo 修改员工初始密码
     * @param id
     * @return
     */
    public int resetPwd(Integer id) {
        Employee employee = new Employee();
        //必须先设置盐值，再给密码重新加密赋值
        employee.setSalt(UUIDUtils.randomUUID());
        employee.setLoginpwd(PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD,employee.getSalt(),SystemConstant.PASSWORD_COUNT));
        //主键员工编号
        employee.setId(id);
        return employeeMapper.updateEmployee(employee);
    }

    /**
     *todo 保存员工角色关系
     * @param roleIds
     * @param empId
     * @return
     */
    public boolean saveEmployeeRole(String roleIds, Integer empId) {
        try {
            //先删除员工角色关系表的数据
            employeeMapper.deleteEmployeeAndRole(empId);
            // 再保存员工角色关系
            String[] idStr = roleIds.split(",");
            for (int i = 0; i < idStr.length; i++) {
                employeeMapper.addEmployeeRole(idStr[i], empId);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
